package com.sbpm.crmbpm.client.deprecforms;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Label;
import com.sbpm.crmbpm.client.services.ProcessService;
import com.sbpm.crmbpm.client.services.ProcessServiceAsync;
import com.sbpm.crmbpm.client.util.SimpleAsyncCallback;
import com.sencha.gxt.core.client.resources.ThemeStyles;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent;

import java.util.Map;

/**
 * Created by TT on 26.10.2014.
 */
public abstract class ProcessForm extends Window
{

    private final ProcessServiceAsync processService = GWT.create( ProcessService.class );

    private Long taskId;
    private String user;
    private OnFinishCallBack onFinishCallBack;

    protected void init( String taskName )
    {
        setModal( true );
        setBlinkModal( true );

        FramedPanel panel = new FramedPanel(  );

        panel.setHeadingText( taskName );
        panel.setBodyStyle( "background: none; padding: 10px" );
        panel.setWidth( 350 );

        createForm( panel );

        addHideHandler( new HideEvent.HideHandler()
        {
            @Override
            public void onHide( HideEvent event )
            {
                //stopTask();
            }
        } );

        addProcessButtons(panel);
        add( panel );
    }

    private void addProcessButtons(FramedPanel panel) {
        addCompleteButtons(panel);


        TextButton cancelButton = new TextButton( "Cancel" );
        cancelButton.addSelectHandler( new SelectEvent.SelectHandler()
        {
            @Override
            public void onSelect( SelectEvent event )
            {
                stopTask();

            }
        } );
        panel.addButton( cancelButton );
    }

    protected void addCompleteButtons(FramedPanel panel) {
        TextButton button = createCompleteButton("OK", new OnCompleteEvent()
        {
            @Override
            public void process( Map< String, Object > data )
            {

            }
        } );

        panel.addButton( button );
    }

    public TextButton createCompleteButton( String label, final OnCompleteEvent completeEvent ) {
        TextButton okButton = new TextButton( label );
        okButton.addSelectHandler( new SelectEvent.SelectHandler()
        {
            @Override
            public void onSelect( SelectEvent event )
            {
                Map<String, Object> data = getData();
                completeEvent.process( data );
                completeTask(data);
            }
        } );
        return okButton;
    }

    private void completeTask( Map<String, Object> data ) {
        processService.completeTask( taskId, user, data, new SimpleAsyncCallback()
        {
            @Override
            public void onSuccess( Object result )
            {
                ProcessForm.this.hide();
                if( onFinishCallBack != null )
                {
                    onFinishCallBack.onFinish();
                }
            }
        } );
    }

    private void stopTask()
    {
        processService.stopTask( taskId, user, new SimpleAsyncCallback()
        {
            @Override
            public void onSuccess( Object result )
            {
                ProcessForm.this.hide();
                if( onFinishCallBack != null )
                {
                    onFinishCallBack.onFinish();
                }
            }
        } );
    }

    protected Label createLabel(String text) {
        Label label = new Label(text);
        label.getElement().getStyle().setProperty("whiteSpace", "nowrap");
        label.getElement().getStyle().setProperty("padding", "5px");
        label.getElement().getStyle().setProperty("margin", "5px");

        label.addStyleName(ThemeStyles.get().style().border());
        label.addStyleName("pad-text white-bg");

        return label;
    }

    public abstract String getTaskDescription();

    public abstract void createForm( FramedPanel panel );

    public abstract Map<String, Object> getData();

    public abstract void setData( Map<String, Object> data );

    public Long getTaskId()
    {
        return taskId;
    }

    public void setTaskId( Long taskId )
    {
        this.taskId = taskId;
    }

    public String getUser()
    {
        return user;
    }

    public void setUser( String user )
    {
        this.user = user;
    }

    public OnFinishCallBack getOnFinishCallBack()
    {
        return onFinishCallBack;
    }

    public void setOnFinishCallBack( OnFinishCallBack onFinishCallBack )
    {
        this.onFinishCallBack = onFinishCallBack;
    }
}
